<?php
/**
 * 微信支付
 *
 * @date    2018-07-26 10:01:35
 * @version $Id$
 */

namespace App\Commons\Services\Payment;

class WechatAdapter extends PayAdapter
{
    public function __construct()
    {
        parent::__construct('wechat');
    }

    protected function convertPayParams(array $options)
    {
        $params = [
            'out_trade_no' => $options['out_trade_no'],
            'body' => $options['subject'],
            'total_fee' => _bcmul($options['total_amount'], 100),   // 微信以分为单位，整型
        ];
        if (isset($options['openid'])) {
            $params['openid'] = $options['openid'];
        }

        return $params;
    }

    public function scan(string $out_trade_no, string $subject, float $total_amount, array $options = [])
    {
        $openid = array_only($options, ['openid']);
        return parent::pay('scan', $out_trade_no, $subject, $total_amount, $openid);
    }

    /**
     *     "appid" => "wx8045f0866bf57d6f"
    "bank_type" => "CFT"
    "cash_fee" => "1"
    "fee_type" => "CNY"
    "is_subscribe" => "N"
    "mch_id" => "1231722302"
    "nonce_str" => "v5eZ9hQQ37kZCtc0"
    "openid" => "xxxxxxxx-w"
    "out_trade_no" => "WE-520ee58b446268938a63825b199a0"
    "result_code" => "SUCCESS"
    "return_code" => "SUCCESS"
    "time_end" => "20180808125919"
    "total_fee" => "1"
    "trade_type" => "NATIVE"
    "transaction_id" => "4200000168201808082507881231"
     * @return [type] [description]
     */
    public function verify()
    {
        $data = parent::verify();
        throw_on($data->result_code !== 'SUCCESS' || $data->return_code !== 'SUCCESS', '微信回调：订单支付失败');
        $data['total_fee'] = _bcdiv(intval($data['total_fee']), 100);
        return $data;
    }
}
